﻿using JxcLite.Helpers;

namespace JxcLite;

/// <summary>
/// 系统配置类。
/// </summary>
public static class AppConfig
{
    private static readonly List<MenuInfo> AppMenus =
    [
        new MenuInfo { Id = "Home", Name = "首页", Icon = "home", Target = "Tab", Url = "/app" },
        new MenuInfo { Id = "Mine", Name = "我的", Icon = "user", Target = "Tab", Url = "/app/mine" },
        new MenuInfo { Id = "Test", Name = "测试模块", Icon = "form", Target = "Menu", Color = "#1890ff", Url = "/app/test" },
        new MenuInfo { Id = "Add", Name = "功能待加", Icon = "plus", Target = "Menu", Color = "#4fa624" }
    ];

    /// <summary>
    /// 取得系统ID。
    /// </summary>
    public static string AppId => "JxcLite";

    /// <summary>
    /// 取得系统名称。
    /// </summary>
    public static string AppName => "进销存管理系统";

    /// <summary>
    /// 添加系统配置模块。
    /// </summary>
    /// <param name="services"></param>
    public static void AddJxcLite(this IServiceCollection services)
    {
        Console.WriteLine(AppName);
        Config.AppMenus = AppMenus;

        var assembly = typeof(AppConfig).Assembly;
        services.AddKnown(info =>
        {
            //项目ID、名称、类型、程序集
            info.Id = AppId;
            info.Name = AppName;
            info.IsSize = true;
            //info.IsLanguage = true;
            info.IsTheme = true;
            info.Assembly = assembly;
            //JS路径，通过JS.InvokeAppVoidAsync调用JS方法
            //info.JsPath = "./script.js";
        });
        services.AddKnownAdmin();

        UIConfig.TagColor = GetTagColor;

        //添加模块
        Config.AddModule(assembly);
    }

    public static void AddJxcLiteCore(this IServiceCollection services)
    {
        var assembly = typeof(AppConfig).Assembly;
        services.AddJxcLite();
        services.AddKnownAdminCore(option =>
        {
            //option.ProductId = "Test";
            //option.CheckSystem = info => Result.Error("无效密钥，请重新授权！");
            option.AddModules(ModuleHelper.AddAppModules);
            option.AddAssembly(assembly);
        });

        services.AddScoped<IHomeService, HomeService>();
        services.AddScoped<IBaseDataService, BaseDataService>();
        services.AddScoped<IBillService, BillService>();
        services.AddScoped<IInventoryService, InventoryService>();
        services.AddScoped<IFinanceService, FinanceService>();
        services.AddScoped<IReportService, ReportService>();
    }

    private static string GetTagColor(string text)
    {
        if (text == BillType.Import) return "blue";
        else if (text == BillType.ImportReturn) return "orange";
        else if (text == BillType.Export) return "success";
        else if (text == BillType.ExportReturn) return "purple";
        else if (text == "现金") return "gold";
        else if (text == "微信") return "green";
        else if (text == "支付宝") return "blue";
        else if (text == "对账") return "processing";
        return "";
    }
}